package modes

import (
	"fmt"
	"strings"
)

/*
 * 描述: 科目 + 课程 + 课次 表
 *
 *****************************/
type Subjects struct {
	Id       int64  `json:"s_id" xorm:"id"`                // 科目ID 1 (一值多意:科目ID and 课次Id and 课程Id)
	PId      int64  `json:"s_pid" xorm:"pid"`              // 项目ID 1
	Name     string `json:"s_name" xorm:"name"`            // 科目名称 1
	At       int64  `json:"s_at" xorm:"at"`                // 时间 1
	Start    int    `json:"s_start" xorm:"start"`          // 状态 1 (0 启用  1 禁用)
	Year     int    `json:"s_year" xorm:"year"`            // 年份 1
	Sort     int    `json:"s_sort" xorm:"sort"`            // 排序(程序步进值:100) [改造]
	Type     int    `json:"s_typ" xorm:"typ"`              // 课程类型[0:无效 1:录播;2直播] [新增]
	CoverUrl string `json:"s_cover_url" xorm:"cover_url"`  // 封面图片 [新增]
	ClassUrl string `json:"s_class_url" xorm:"course_url"` // 描述图片 [新增]
	HasFree  int    `json:"s_has_free" xorm:"has_free"`    // 免费课(comb 含试听课)(1:不免费; 2:免费) [新增]

	// comb 总课次
	// comb 含试听课
	// comb 含讲义数量
	// comb 被多少班型关联
}

func (this *Subjects) TableName() string {
	return "subject"
}
func (this *Subjects) Load() (bool, error) {
	return Db(0).Get(this)
}
func (this *Subjects) SbjName(sid int64) ([]Subjects, error) {
	swhere := fmt.Sprintf("id  = %d ", sid)
	list := make([]Subjects, 0)
	err := Db(0).Where(swhere).Find(&list)
	return list, err
}

func (this *Subjects) Create() (int64, error) {
	return Db(0).Insert(this)
}
func (this *Subjects) Update(id int64, columns ...string) (int64, error) {
	return Db(0).ID(id).MustCols(columns...).Update(this)
}
func (this *Subjects) Delete(id int64) (int64, error) {
	return Db(0).ID(id).Delete(&Subject{})
}
func (this *Subjects) Orders() map[string]string {
	return map[string]string{}
}
func (this *Subjects) GetUseList() ([]Subjects, error) {
	list := make([]Subjects, 0)
	err := Db(0).Where(" id%100000=0 and start=0").Find(&list)
	return list, err
}

//SELECT  DISTINCT (sid-sid%1000)  FROM `collen`
func (this *Subjects) GetListByidList(idList []int64) ([]Subjects, error) {
	list := make([]Subjects, 0)
	err := Db(0).In("id", idList).Where("start=0").Find(&list)
	return list, err
}

type SubjectsAudition struct {
	Subjects `xorm:"extends"`
	PlayCl   []PlayClass `xorm:"-"`
}

func (this *SubjectsAudition) GetSId(pid int64) ([]SubjectsAudition, error) {
	list := make([]SubjectsAudition, 0)
	swhere := fmt.Sprintf("pid = %d  and start = 0 ", pid)
	err := Db(1).Table("subject").Where(swhere).Where(" id%100000=0").Find(&list)
	fmt.Println(list)
	//var su PlayClass
	//su.PPId = pid
	//allAuditionList, err := su.GetAllAudition()
	//for i, row := range list {
	//	for j, _ := range allAuditionList {
	//		fmt.Println(row.Id,"row.Id----------")
	//
	//		if allAuditionList[j].SSId-allAuditionList[j].SSId%100000 == row.Id {
	//			if list[i].PlayCl == nil {
	//				list[i].PlayCl = make([]PlayClass, 0)
	//			}
	//			list[i].PlayCl = append(list[i].PlayCl, allAuditionList[i])
	//
	//		}
	//	}
	//}
	for i, row := range list {
		var su PlayClass
		list[i].PlayCl, err = su.NewAudition(row.Id)
	}
	return list, err
}

type JoinPlayClass struct {
	Id        int64 `json:"s_id" xorm:"id"` //科目ID
	Subjects  `xorm:"extends"`
	Project   `xorm:"extends"`
	PlayClass `xorm:"extends"`
}

func (this *JoinPlayClass) KcIdList(val *Pager) error {
	list := make([]JoinPlayClass, 0)
	val.TName = "subject"
	val.Page--
	if val.Page <= 0 {
		val.Page = 0
	}
	var field, sort, paixu string
	if this.Id == 0 {
		field = fmt.Sprintf("%s.id %% 100000 = 0 and project.start = 0  and %s.start = 0", val.TName, val.TName)
	} else if (this.Id % 10000) == 0 {
		field = fmt.Sprintf("%s.id > %d and %s.id < %d  and project.start = 0 and  %s.start = 0 ", val.TName, this.Id, val.TName, this.Id+1000, val.TName)
	} else {
		field = fmt.Sprintf("%s.id > %d and %s.id < %d and project.start = 0 and %s.start = 0", val.TName, this.Id, val.TName, this.Id+1000, val.TName)
	}
	val.Total, _ = Db(1).Table(val.TName).
		Join("INNER", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		Join("INNER", "playclass", fmt.Sprintf("%s.id = playclass.sid", val.TName)).
		Where(field).
		Where(val.ToWhere()).
		Count()

	paixu = fmt.Sprintf("%s.id", val.TName) // 默认ID

	if 1 == val.Rules {
		sort = fmt.Sprintf("%s desc", paixu)
	} else {
		sort = fmt.Sprintf("%s asc", paixu)

	}
	err := Db(1).Table(val.TName).
		Join("LEFT", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		Join("LEFT", "playclass", fmt.Sprintf("%s.id = playclass.sid", val.TName)).
		Where(field).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}

// 取学员所有班型下直播课？？？
type SubjectClass struct {
	SubClass `xorm:"extends"`
	Subjects `xorm:"extends"`
}
//修改禁用课程不可展示
func (this *SubjectClass) SubjectSubClass(sli []int64) ([]int64, error) {
	list := []int64{}
	err := Db(1).Table("c_subject").
		Join("LEFT", "subject", fmt.Sprintf("%s.s_id = subject.id", "c_subject")).
		Cols("s_id").
		In("c_id", sli).
		Where("subject.typ = 2 and subject.start = 0").
		Find(&list)
	return list, err
}

// 取学员所有班型下直播课按课程
type SubjectCurr struct {
}

func (this *SubjectCurr) SubjectSubClass(sli []int64) ([]int64, error) {
	list := []int64{}
	err := Db(1).Table("c_subject").
		Join("LEFT", "subject", fmt.Sprintf("%s.s_id = subject.id", "c_subject")).
		Cols("s_id").
		In("c_id", sli).
		Where("subject.typ = 2 and subject.start = 0").
		Find(&list)
	return list, err

}

type SubSubject struct {
	Id       int64  `json:"s_id" xorm:"id"`               //科目id
	Name     string `json:"s_nmae" xorm:"name"`           //科目名称
	ClassUrl string `json:"course_url" xorm:"course_url"` //描述图片
	Year     int    `json:"s_year" xorm:"year"`           //年份
}

func (this *SubSubject) SubjectSId(sil []int64) ([]SubSubject, error) {
	list := make([]SubSubject, 0)
	newSli := make([]int64, 0)
	for _, v := range sil {
		newV := v - v%100000
		newSli = append(newSli, newV)
	}
	return list, Db(0).Table("subject").Cols("id,name").In("id", newSli).Find(&list)
}

func (this *SubSubject) SubjectSSId(sil []int64) ([]SubSubject, error) {
	list := make([]SubSubject, 0)
	return list, Db(0).Table("subject").Cols("id,name,course_url,year").In("id", sil).Find(&list)
}

type LiveCount struct {
	Id  int64 `json:"s_id" xorm:"s_id"`       //科目id
	Num int   `json:"s_count" xorm:"s_count"` //数量
}

func (this *LiveCount) LiveClsCount(ids []int64) ([]LiveCount, error) {
	list := make([]LiveCount, 0)
	swhere := fmt.Sprintf("states = 2 or states = 3 ")
	Db(0).Table("liveclass").Select(" s_id ,COUNT(*) as s_count ").In("s_id", ids).Where(swhere).GroupBy("s_id").Find(&list)
	return list, nil
}

type LiveTName struct {
	Id    int64  `json:"s_id" xorm:"s_id"`     //科目id
	Tname string `json:"t_name" xorm:"t_name"` //名称
}

func (this *LiveTName) LiveClsTName(ids []int64) ([]LiveTName, error) {
	list := make([]LiveTName, 0)
	err := Db(0).SQL(fmt.Sprintf("SELECT DISTINCT s_id,GROUP_CONCAT(DISTINCT instructor) as t_name from liveclass where s_id in (%v) GROUP BY s_id", strings.Replace(strings.Trim(fmt.Sprint(ids), "[]"), " ", ",", -1))).Find(&list)
	return list, err

}

type PlaySub struct {
	Id       int64  `json:"id" xorm:"id"`                //课程id
	Year     int64  `json:"yest" xorm:"year"`            //年份
	ClassUrl string `json:"class_url" xorm:"course_url"` //描述图片
}

func (this *PlaySub) PlaySubjects(id []int64) ([]PlaySub, error) {
	list := make([]PlaySub, 0)
	return list, Db(0).Table("subject").Cols("id,year,course_url").In("id", id).Find(&list)
}

//项目下禁用的版型

func (this *Subjects) ProSidGet(pid []string) ([]int64,error ) {
	list := []int64{}
	err := Db(1).Table("subject").Cols("id").In("pid",pid).Where("id%1000 = 0 and start = 1").Find(&list)
	return list,err
}

//科目下禁用的版型
func (this *Subjects) ProSidIds(sid,s_id int64) ([]int64,error) {
	list := []int64{}
	swhere := fmt.Sprintf("id > %d and id < %d and start != 0",sid,s_id)
	err := Db(1 ).Table("subject").Cols("id").Where(swhere).Find(&list)
	return list,err
}